#include "HttpServer.hpp"

void Login(HttpRequest& req,HttpResponse* resp)
{
    // 登录功能：根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入到了登录模块：" << req.Path() << ", " << req.Args();
    std::string req_args = req.Args();
    // 能干的事情？
    // 1. 解析参数格式，得到要的参数
    // 2. 访问数据库，验证对应的用户是否是合法的用户，其他工作...
    // 3. 登录成功：自动跳转到首页
    // resp->SetCode(302);
    // resp->SetHeader("Location", "/");

    string body = req.GetContent("wwwroot/success.html");  // 登陆成功后的页面
    resp->SetCode(200);
    resp->SetHeader("Content-Length", std::to_string(body.size()));
    resp->SetHeader("Content-Type", "text/html");
    resp->SetBody(body);
}

void Register(HttpRequest& req,HttpResponse* resp)
{
    LOG(LogLevel::DEBUG) << "进入到了注册模块：" << req.Path() << ", " << req.Args();
}

void Search(HttpRequest& req,HttpResponse* resp)
{
    // 登录功能：根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入到了搜索模块：" << req.Path() << ", " << req.Args();
}

void Test(HttpRequest& req,HttpResponse* resp)
{
    // 登录功能：根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入到了测试模块：" << req.Path() << ", " << req.Args();
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << endl;
        return 1;
    }
    auto httpserver = make_unique<HttpServer>(std::atoi(argv[1]));
    // 要让服务器具有登录功能
    httpserver->Register("/login", Login);  // restful风格的请求方式
    httpserver->Register("/register", Register);
    httpserver->Register("/search", Search);
    httpserver->Register("/test", Test);

    httpserver->Start();
    return 0;
}